/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.generalfile.servlet;

import cn.net.greenet.common.db_service.ConfigManager;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.ProCharacterLibrary;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * 上传用户上传的协议特征库文件
 *
 * @author ShiYun,2014/11/04
 */
@WebServlet(name = "UplodProtocolBank2DB", urlPatterns = {"/common/upload_protocol_bank"})
public class UplodProtocolBank2DB extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String path = request.getParameter("path");
        String MessageType = request.getParameter("MessageType");
        String policy_instance = request.getParameter("policy_instance");
        GeneralFileProcess pro = new GeneralFileProcess(path, MessageType, policy_instance);
//        String name = ConfigManager.getInstance().getString("protocolBankFileName");
        path = pro.saveFile(request, "");
        File src = new File(path);
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        session.clear();
        ProCharacterLibrary p = new ProCharacterLibrary();
        p.setName(src.getName());
        p.setPath(path);
        Date d = new Date();
        p.setUptime(new java.sql.Date(d.getTime()));
        session.save(p);
        session.close();
        //获取文件路径和指定文件名
//        pro.getProperties();
//        String dstPath = pro.getCommonFilePath();
//        String filename = pro.getProtocolFeatureLibrary();
//        System.out.println("dstPath:" + dstPath);
//        if (dstPath.endsWith("/")) {
//            dstPath = dstPath + filename;
//        } else {
//            dstPath = dstPath + File.separator + filename;
//        }
//        FileUtils.copyFile(src, new File(dstPath));
        boolean result = true;
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println(result);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
